﻿namespace PMS.Client.FrmBackup
{
    using AsyncFactory;
    using PMS.BLL;
    using PMS.Common;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmAdd : Form
    {
        private TextBox backupPath;
        private ComboBox backupType;
        private Button btnBackup;
        private Button btnCancel;
        private IContainer components = null;
        private Label label1;
        private Label label2;

        public FrmAdd()
        {
            this.InitializeComponent();
        }

        private void backupType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.backupType.SelectedIndex == 0)
            {
                this.backupPath.Text = Application.StartupPath + @"\" + Config.BackupPath + @"\db_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak";
            }
            else if (this.backupType.SelectedIndex == 1)
            {
                this.backupPath.Text = Application.StartupPath + @"\" + Config.BackupPath + @"\log_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak";
            }
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            bool success = false;
            int index = this.backupType.SelectedIndex;
            string path = this.backupPath.Text;
            AsyncProcess.Process(() => success = new BackupBLL().Add(index, path), delegate {
                if (success)
                {
                    SyslogBLL.Write("系统备份", "数据备份成功");
                    MessageBox.Show("备份成功！");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("备份失败！");
                }
            }, true, LoadingImageType.Strip);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmAdd_Load(object sender, EventArgs e)
        {
            this.backupType.Items.Add("数据库");
            this.backupType.Items.Add("日志");
            this.backupType.SelectedIndex = 0;
            this.backupPath.Text = Application.StartupPath + @"\" + Config.BackupPath + @"\db_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak";
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmAdd));
            this.label1 = new Label();
            this.backupType = new ComboBox();
            this.label2 = new Label();
            this.backupPath = new TextBox();
            this.btnCancel = new Button();
            this.btnBackup = new Button();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x29, 0x26);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x35, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "备份类型";
            this.backupType.DropDownStyle = ComboBoxStyle.DropDownList;
            this.backupType.FormattingEnabled = true;
            this.backupType.Location = new Point(100, 0x23);
            this.backupType.Name = "backupType";
            this.backupType.Size = new Size(0x85, 20);
            this.backupType.TabIndex = 1;
            this.backupType.SelectedIndexChanged += new EventHandler(this.backupType_SelectedIndexChanged);
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x2b, 0x4c);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x35, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "备份目录";
            this.backupPath.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.backupPath.BorderStyle = BorderStyle.FixedSingle;
            this.backupPath.Location = new Point(100, 0x4a);
            this.backupPath.Name = "backupPath";
            this.backupPath.ReadOnly = true;
            this.backupPath.Size = new Size(0x106, 0x15);
            this.backupPath.TabIndex = 3;
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x11f, 120);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 8;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnBackup.BackColor = Color.White;
            this.btnBackup.FlatStyle = FlatStyle.Popup;
            this.btnBackup.Location = new Point(100, 120);
            this.btnBackup.Name = "btnBackup";
            this.btnBackup.Size = new Size(0x4b, 0x17);
            this.btnBackup.TabIndex = 7;
            this.btnBackup.Text = "备份";
            this.btnBackup.UseVisualStyleBackColor = false;
            this.btnBackup.Click += new EventHandler(this.btnBackup_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x18e, 0xa9);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnBackup);
            base.Controls.Add(this.backupPath);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.backupType);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmAdd";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "数据备份";
            base.Load += new EventHandler(this.FrmAdd_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

